const express=require('express');
// 创建路由
const route=express.Router()
//引入MD5加密模块
const md5=require('md5')
//引入管理员集合
const adminModel = require('../db/model/adminModel')
//引入正则校验中间件
const addAdminReg =require('../middleware/addAdminReg');
const isLoginMiddle = require('../middleware/isLoginMiddle');
// 创建post请求的路由，用来接受添加管理员的相关信息
route.post("/addAdmin",addAdminReg,isLoginMiddle,async (req, res) => {
    // 结构请求体，属性名一定要一致
    const {
      adminName,
      password,
      regTime,
    } = req.body
    // console.log(adminName, password, regTime);
    try {
      const findResult = await adminModel.findOne({
        adminName,
      })
      if (findResult) {
        return res.send({
          code: 10001,
          msg: "添加管理员-用户已经被注册",
          data: null
        });
      }
      await adminModel.create({
        adminName,
        password:md5(password+'xyue'),
        regTime
      })
      res.send({
        code: 10000,
        msg: "添加管理员-添加成功",
        data: null
      })
    } catch (e) {
      res.send({
        code: 10002,
        msg: "添加管理员失败，服务端异常",
        data: null
      })
      console.log("服务端异常",e);
    }
  })
module.exports=route;